package com.aioz.security.config;

import com.aioz.security.filter.JwtAuthenticationTokenFilter;
import com.aioz.security.handler.AccessDeniedHandlerImpl;
import com.aioz.security.handler.AuthenticationEntryPointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig{


    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired

    AuthenticationEntryPointImpl authenticationEntryPoint;

    @Autowired
    AccessDeniedHandlerImpl accessDeniedHandler;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问
                .antMatchers("/user/login").anonymous()
                .antMatchers("/category-menu/*").permitAll()
                .antMatchers("/position/hot/top/{currentPage}").permitAll()
                .antMatchers("/position/get/all").permitAll()
                .antMatchers("/company/get/all").permitAll()
                .antMatchers("/company/get/hot/{currentPage}").permitAll()
                .antMatchers("/company/get/info/{id}").permitAll()
                .antMatchers("/file/company/photos/{name}").permitAll()
                .antMatchers("/position/like/{id}").permitAll()
                .antMatchers("/captcha").permitAll()
                .antMatchers("/user/export").permitAll()
                .antMatchers("/user/get/resume/{id}").permitAll()
                .antMatchers("/user/register").permitAll()
                .antMatchers("/notify/publish").permitAll()
//                .antMatchers("/notify/of/follow/{lastId}/{offset}").permitAll()
//                .antMatchers("/follow/{companyId}/{isFollow}").permitAll()
//                .antMatchers("/follow/or/not/{companyId}").permitAll()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //告诉security如何处理异常
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);
        //允许跨域
        http.cors();
        return http.build();
    }

    @Autowired
    private AuthenticationConfiguration authenticationConfiguration;

    @Bean
    public AuthenticationManager authenticationManager() throws Exception{
        AuthenticationManager authenticationManager = authenticationConfiguration.getAuthenticationManager();
        return authenticationManager;
    }

}
